package com.cockybook.poemspider.model;

import com.cockybook.poemspider.beans.Poet;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 这个类是利用诗人的简介，来体现各种信息的
 */
public class IntroductionModel {

    private static Pattern authorBirthDeathday = Pattern.compile("（.{2,15}[—,～].{2,15}）");
    private static Pattern authorZi = Pattern.compile("字.{1,7}[。,、,，]");
    private static Pattern authorHao = Pattern.compile("号.{1,7}[。,、,，]");


    public static Poet filterAll(String introduction, Poet poet) {
        authorBDDayFilter(introduction, poet);
        authorZiFilter(introduction,poet);
        authorHaoFilter(introduction,poet);
        return poet;
    }


    public static void authorBDDayFilter(String introduction, Poet poet) {


        try {
            Matcher mat = authorBirthDeathday.matcher(introduction);

            if (mat.find()) {
                String value = mat.group();
                value= value.substring(1,value.length()-1);
                int index = value.indexOf("—");
                if(index==-1){
                    index = value.indexOf("～");
                }

                poet.setBirthdy(value.substring(0,index));
                poet.setDeathday(value.substring(index+1));


            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    public static void authorZiFilter(String introduction, Poet poet) {
        try {
            Matcher mat = authorZi.matcher(introduction);

            if (mat.find()) {
                String value = mat.group();
                poet.setZi(value.substring(1,value.length()-1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void authorHaoFilter(String introduction, Poet poet) {
        try {
            Matcher mat = authorHao.matcher(introduction);

            if (mat.find()) {
                String value = mat.group();
                poet.setHao(value.substring(1,value.length()-1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
